package datalookin.fragment;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.DatePicker;
import android.widget.ListView;
import android.widget.Spinner;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.VolleyError;
import com.google.gson.Gson;
import com.whu.zj.MyApplication;
import com.whu.zj.R;
import com.whu.zj.UIHandler;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import datalookin.activity.MainWaterActivity;
import datalookin.activity.MainWaterActivity2;
import datalookin.adapter2.HistoryPlantAdapter2;
import datalookin.dto.WaterPlantDto;
import datalookin.network2.GetWaterPlantData;
import inter.Delegate;
import main.dto.WaterPlant;
import utils.TimeUtil;

/**
 * 历史厂区数据
 * Created by Administrator on 2016/6/5.
 */
public class HistoryPlantFragment extends Fragment {

    ListView lv_history;

    Spinner spinner_type;

    String selectedType;

    TextView tv_date;

    TextView tv_date2;

    String str_date = TimeUtil.currentDate();
    String str_date2 = TimeUtil.currentDate();
    private View btn_search;


    TextView tv_emptyView;

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {

        View view = inflater.inflate(R.layout.fragment_historyplant,null);
        initview(view);
        return view;
    }

    ArrayAdapter<String> adapter = new ArrayAdapter<String>(MyApplication.share(), R.layout.simple_spinner_item);
    {
//        adapter.addAll(MyApplication.getRealPlantType());
        adapter.addAll(MyApplication.someType);

    }


    View.OnClickListener onClickListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            if (v == tv_date)
            {
                pickDate();
            }

            else if (v == tv_date2)
            {
                pickDate2();
            }

            else if (v == btn_search)
            {
                search();
            }
        }
    };

    private void search() {
        getWaterPlantData.setBeginTime(str_date.replace("-","/"));
        getWaterPlantData.setEndTime(str_date2.replace("-","/"));
//        String spilt = "\\(";
//        String type = selectedType.split(spilt)[0];
//        String final_type = type;
//        if (type.contains("瞬时流量")||type.contains("供水量"))
//        {
////            if (type.contains("瞬时流量"))
////            {
////                final_type = type.split("瞬时流量")[0];
////            }
////            if (type.contains("供水量"))
////            {
////                final_type = type.split("供水量")[0];
////            }
//
//            String flow1 = MyApplication.mobileUserDto.getNametoname().getFlowmeter1();
//            if (flow1 != null && final_type.contains(flow1))
//            {
//                final_type = final_type.replace(flow1,"流量计1");
//            }
//
//            String flow2 = MyApplication.mobileUserDto.getNametoname().getFlowmeter2();
//            if (flow2 != null && final_type.contains(flow2))
//            {
//                final_type = final_type.replace(flow2,"流量计2");
//            }
//
//            String flow3 = MyApplication.mobileUserDto.getNametoname().getFlowmeter3();
//            if (flow3 != null && final_type.contains(flow3))
//            {
//                final_type = final_type.replace(flow3,"流量计3");
//            }
//
//            String flow4 = MyApplication.mobileUserDto.getNametoname().getFlowmeter4();
//            if (flow4 != null && final_type.contains(flow4))
//            {
//                final_type = final_type.replace(flow4,"流量计4");
//            }
//
//            String flow5 = MyApplication.mobileUserDto.getNametoname().getFlowmeter5();
//            if (flow5 != null && final_type.contains(flow5))
//            {
//                final_type = final_type.replace(flow5,"流量计5");
//            }
//
//            String flow6 = MyApplication.mobileUserDto.getNametoname().getFlowmeter6();
//            if (flow6 != null && final_type.contains(flow6))
//            {
//                final_type = final_type.replace(flow6,"流量计6");
//            }
//        }

        MainWaterActivity2 mainWaterActivity = (MainWaterActivity2) getActivity();

//        getWaterPlantData.setType(final_type);
        getWaterPlantData.setType(selectedType);
//        getWaterPlantData.setWaterPlantCode(MyApplication.mobileUserDto.getWaterUser().getWaterplantNum());
//        getWaterPlantData.setWaterPlantName(MyApplication.getRealPlant().getWaterPlantName());

        getWaterPlantData.setWaterPlantCode(mainWaterActivity.getWaterPlantNum());
        getWaterPlantData.setWaterPlantName(mainWaterActivity.getWaterPlantName());
        getWaterPlantData.get();

        ((UIHandler.BaseActivity) getActivity()).showDialog();
    }



    private void initview(View view) {
        lv_history = (ListView) view.findViewById(R.id.id_lv_historyplant);
        lv_history.setAdapter(historyPlantAdapter2);
        spinner_type = (Spinner) view.findViewById(R.id.id_spinner_type);
        spinner_type.setAdapter(adapter);
        spinner_type.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
            @Override
            public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
                selectedType = MyApplication.someType.get(i);
            }

            @Override
            public void onNothingSelected(AdapterView<?> adapterView) {

            }
        });


        tv_date = (TextView) view.findViewById(R.id.id_tv_date);

//        SpannableString msp = new SpannableString(str_date);
//        msp.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bluelight)), 0, str_date.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色
//        msp.setSpan(new UnderlineSpan(), 0, str_date.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_date.setText(str_date);
        tv_date.setOnClickListener(onClickListener);

        tv_date2 = (TextView) view.findViewById(R.id.id_tv_date2);
//        tv_date2.setText(TimeUtil.currentDate());
//        SpannableString msp2 = new SpannableString(str_date2);
//        msp2.setSpan(new ForegroundColorSpan(getResources().getColor(R.color.bluelight)), 0, str_date2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);  //设置前景色为洋红色
//        msp2.setSpan(new UnderlineSpan(), 0, str_date2.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        tv_date2.setText(str_date2);
        tv_date2.setOnClickListener(onClickListener);

        btn_search = view.findViewById(R.id.id_btn_search);
        btn_search.setOnClickListener(onClickListener);

        tv_emptyView = (TextView) view.findViewById(R.id.id_tv_empty);
        lv_history.setEmptyView(tv_emptyView);
    }

    private void pickDate() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 指定一个日期
        Date date = null;
        try {
            date = dateFormat.parse(str_date + " "+ "00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 对 calendar 设置为 date 所定的日期
        c.setTime(date);
        Dialog dialog = new DatePickerDialog(
                getActivity(),
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker dp, int year, int month, int dayOfMonth) {
                        setDate(year, month, dayOfMonth);
                    }
                },
                c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        dialog.show();
    }

    private void setDate(int year, int month, int dayOfMonth) {
        String str = "";
        int i = month + 1;
        if (i >=0 && i <=9)
        {
            str = "0"+i;
        }
        else
        {
            str = i+"";
        }

        String dayofmonth_0 = "";
        if (dayOfMonth < 10)
            dayofmonth_0 = "0";

        str_date = year+"-"+str+"-"+ dayofmonth_0 + dayOfMonth;
        tv_date.setText(str_date);
//        TextUtil.setLinkLike(tv_date, str_date);

    }



    private void pickDate2() {
        Calendar c = Calendar.getInstance();
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
        // 指定一个日期
        Date date = null;
        try {
            date = dateFormat.parse(str_date2 +" " + "00:00:00");
        } catch (ParseException e) {
            e.printStackTrace();
        }
        // 对 calendar 设置为 date 所定的日期
        c.setTime(date);
        Dialog dialog = new DatePickerDialog(
                getActivity(),
                new DatePickerDialog.OnDateSetListener() {
                    public void onDateSet(DatePicker dp, int year,int month, int dayOfMonth) {
                        setDate2(year,month,dayOfMonth);
                    }
                },
                c.get(Calendar.YEAR), // 传入年份
                c.get(Calendar.MONTH), // 传入月份
                c.get(Calendar.DAY_OF_MONTH) // 传入天数
        );
        dialog.show();
    }

    private void setDate2(int year, int month, int dayOfMonth) {
        String str = "";
        int i = month + 1;
        if (i >=0 && i <=9)
        {
            str = "0"+i;
        }
        else
        {
            str = i+"";
        }

        String dayofmonth_0 = "";
        if (dayOfMonth < 10)
            dayofmonth_0 = "0";
        str_date2 = year+"-"+str+"-"+dayofmonth_0+dayOfMonth;
        tv_date2.setText(str_date2);
//        TextUtil.setLinkLike(tv_date2, str_date2);
    }




    List<WaterPlantDto> waterPlantDtos = new ArrayList<WaterPlantDto>();
    HistoryPlantAdapter2 historyPlantAdapter2 = new HistoryPlantAdapter2();
    {
        historyPlantAdapter2.setWaterPlantDtos(waterPlantDtos);
    }

    Delegate waterPlantDtoResponse = new Delegate() {
        @Override
        public void onResponse(String response) {
            if (!response.startsWith("[")){
                finishIt();
                return;
            }
            waterPlantDtos.clear();
            List<WaterPlantDto> tmp = JSON.parseArray(response,WaterPlantDto.class);
            waterPlantDtos.addAll(tmp);
            historyPlantAdapter2.notifyDataSetChanged();
            finishIt();
        }

        private void finishIt() {
            ((UIHandler.BaseActivity) getActivity()).closeDialog();
        }

        @Override
        public void onErrorResponse(VolleyError error) {
            finishIt();
        }
    };

    GetWaterPlantData getWaterPlantData = new GetWaterPlantData();
    {
        getWaterPlantData.setDelegate(waterPlantDtoResponse);
    }


    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

    }

    
}
